package com.sjz.han.industry.base;

import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.sjz.han.industry.R;
import com.sjz.han.industry.dialogs.DialogHelper;
import com.sjz.han.industry.utils.ShareUtils;
import com.sjz.han.industry.utils.StatusBarTextColorUtils;
import com.sjz.han.industry.weight.MultiStateView;
import com.sjz.han.industry.weight.SimpleMultiStateView;


/**
 * desc:
 * author: WRB .
 * date: 2017/9/2 .
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Dialog mLoadingDialog = null;
    //================标题栏部分=======================
    public RelativeLayout titlebar_rel;//标题背景
    public TextView titleTv;//标题
    public RelativeLayout reback_btn;//返回按钮
    //================标题栏部分=======================
    public ShareUtils shareUtils;
    public Bundle savedInstanceState;

    public SimpleMultiStateView mSimpleMultiStateView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState=savedInstanceState;
        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        MyApp.getInstance().addActivity(this);
        shareUtils = new ShareUtils(this);
        setContentView(setLayoutId());
        setStatusBar();//设置浸入式
        mSimpleMultiStateView=(SimpleMultiStateView) this.findViewById(R.id.SimpleMultiStateView);
        initStateView();
        mLoadingDialog = DialogHelper.getLoadingDialog(this);
        initViews();
        initDatas();
        setLisener();
    }
    //设置布局
    protected abstract int setLayoutId();
    protected abstract void initDatas();
    protected abstract void initViews();
    protected abstract void setLisener();

    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, null);
        StatusBarTextColorUtils.statusBarLightMode(this);
    }


    protected void showLoadingDialog() {
        if (mLoadingDialog != null)
            mLoadingDialog.show();
    }

    protected void showLoadingDialog(String str) {
        if (mLoadingDialog != null) {
            TextView tv = (TextView) mLoadingDialog.findViewById(R.id.tv_load_dialog);
            tv.setText(str);
            mLoadingDialog.show();
        }
    }

    protected void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApp.getInstance().removeActivity(this);
    }

    private void initStateView() {
        if (mSimpleMultiStateView == null) return;
        mSimpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                        onRetry();
                    }
                });
    }
    protected abstract void onRetry();

    public void showLoading() {
        if (mSimpleMultiStateView != null) {
            mSimpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (mSimpleMultiStateView != null) {
            mSimpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (mSimpleMultiStateView != null) {
            mSimpleMultiStateView.showErrorView();
        }
    }

    public void showEmptyData() {
        if (mSimpleMultiStateView != null) {
            mSimpleMultiStateView.showEmptyView();
        }
    }
}
